<?php
/*
PWision extension: Newspaper http://pw-newspaper.googlecode.com/
Copyright (C) 2010 Becheru Petru-Ioan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once("inc/PRedirectSection.inc");
include_once("pw-newspaper.inc");
/** 	The CreateArticle class is designed to model a section that allows a user to create articles. It creates an empty article and redirects to EditArticle section.
	*	\latexonly  \label{PWision:Extensions:Newspaper:CreateArticle} \index{extension} \index{newspaper} \index{article} \endlatexonly
	*	\brief an article creator section.
	*	\version 2.2.2
	*/
class CreateArticle extends PRedirect2Section
	{
	function setParams()
		{
		$this
			->addParam('subarticleID','/^[0-9]{1,10}$/','')
			->addParam('clone','/^[0-9]{1,10}$/','')
			;
		}
	function CreateArticle()
		{
		$article=new ArticleInfo;
			$article->Title		='Lorem ipsum';
			$article->text			='[p]Dorem ipsum dolor sit amet, consectetuer adipiscing elit sed diam nonummy nibh commodo consequat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh commodo consequat.[/p]';
			$article->authors[]	='no one';
			$article->keywords[]	='empty article';
			$article->logs[]		=date('Y-m-d H:i:s')." log start\n";
		if(
			!( $article->ID = NewsPaper::CreateArticle($article) )
			)
			{
			$this->AddMessage("Article NOT created!");
			PRedirect2Section::PRedirect2Section("Root");
			return;//if a new article could not be created, return now because future instructions are useless
			}
		$this->AddMessage(
			'Article created as '.
				Sections::Link2(
					'Article',
					$article->Title,
					$article->Title,
					Object(new PURLAttributes)
						->setAtr('ID',$article->ID)
					)->Draw()
			);
		PRedirect2Section::PRedirect2Section(
			"EditArticle"
			, Object(new PURLAttributes)
					->setAtr('ID',$article->ID)
			);
		if( $this->wasParamSet('subarticleID') )
			{
			$article->owners[]=$this->getParam('subarticleID');
			NewsPaper::EditArticle($article);
			}

		if($this->wasParamSet('clone') &&
			$clone=NewsPaper::getArticleInfo( $this->getParam('clone') )
			)
			{
			//prezerving ID and owners from $article
			$clone->ID=$article->ID;
			$clone->owners=$article->owners;
			//no sub_articles for the clone
			$clone->sub_articles=null;
			NewsPaper::EditArticle($clone);//clone magic happens here
			}
		}
	function Chapter1Title()
		{
		return PRedirect2Section::Chapter1Title()
			->Insert( new PText("Article created.") );
		}
	}

?>